/**
 * 
 */
package com.deitel.advjhtp1.gui.webbrowser;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/**
 * @author Aslanbek
 *
 */
public class WebToolBar extends JToolBar implements HyperlinkListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4716392883227385798L;
	private WebBrowserPane webBrowserPane;
	private JButton backButton;
	private JButton forwardButton;
	private JTextField urlTextField;
	
	public WebToolBar(WebBrowserPane browser)
	{
		super("Web Navigation");
		webBrowserPane = browser;
		webBrowserPane.addHyperlinkListener(this);
		
		urlTextField = new JTextField(25);
		urlTextField.addActionListener(
				new ActionListener() {
		
					public void actionPerformed(ActionEvent e) {
						try{
							URL url = new URL(urlTextField.getText());
							webBrowserPane.goToURL(url);
						}
						
						catch(MalformedURLException urlException){
							urlException.printStackTrace();
						}
					}
				}
			);
		
		backButton = new JButton("<<.");//TODO new JButton(new ImageIcon(getClass().getResource("images/back.gif"))); 
		backButton.addActionListener(
				new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
					URL url = webBrowserPane.back();
					
					urlTextField.setText(url.toString());
						}
					}
				);
		forwardButton = new JButton(".>>");//TODO new JButton(new ImageIcon(getClass().getResource("images/forward.ico")));
		
		forwardButton.addActionListener(
				new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0) {
						URL url = webBrowserPane.forward();
						
						urlTextField.setText(url.toString());
						}
					}
				);
		
		add(backButton);
		add(forwardButton);
		add(urlTextField);
	}
	
	public void hyperlinkUpdate(HyperlinkEvent event) {
		
		if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
		{
			URL url = event.getURL();
			
			webBrowserPane.goToURL(url);
			urlTextField.setText(url.toString());
		}
	}

}
